package com.achilles.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by AchillesL on 2016-9-20.
 */
public class DBHelper extends SQLiteOpenHelper {

    private static final int VERSION = 1;
    private static final String DB_NAME = "PlayTask.db";
    public static final String USER_TABLE_NAME = "UserTable";
    public static final String PROJECT_TABLE_NAME = "ProjectTable";
    public static final String PROJECT_HISTORY_TABLE_NAME = "ProjectHistoryTable";
    public static final String GIFT_TABLE_NAME = "GiftTable";
    public static final String GIFT_HISTORY_TABLE_NAME = "GiftHistoryTable";
    public static final String Spare_TABLE_NAME = "SpareTable";
    public static final String APP_INFO_TABLE = "AppInfoTable";

    public DBHelper(Context context) {
        this(context, DB_NAME, VERSION);
    }

    public DBHelper(Context context, String name, int version) {
        this(context, name, null, version);
    }

    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int
            version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createUserTable(db);
        createProjectTable(db);
        createHistoryProjectTable(db);
        createGiftTable(db);
        createHistoryGiftTable(db);
        createAppTable(db);
        createSpareTable(db);
    }

    private void createSpareTable(SQLiteDatabase db) {
        db.execSQL("create table " + DBHelper.Spare_TABLE_NAME + "(" +
                "id int identity(0,1)," +
                "data0 text," +
                "data1 text," +
                "data2 text," +
                "data3 text," +
                "data4 text," +
                "data5 text," +
                "data6 text," +
                "data7 text," +
                "data8 text" +
                ")");
    }

    private void createHistoryGiftTable(SQLiteDatabase db) {
        db.execSQL("create table " + DBHelper.GIFT_HISTORY_TABLE_NAME + "(" +
                "id int identity(0,1)," +
                "giftName text," +
                "giftCreateTime text," +
                "giftCompleteDate text," +
                "giftCompleteSpecificTime text," +
                "giftScore int," +
                "giftType int," +
                "isCompleted int," +
                "otherInfo text," +
                "giftOrder int" +
                ")");
    }

    private void createHistoryProjectTable(SQLiteDatabase db) {
        db.execSQL("create table " + DBHelper.PROJECT_HISTORY_TABLE_NAME + "(" +
                "id int identity(0,1)," +
                "projectName text," +
                "projectNote text," +
                "projectFixNote int," +
                "projectType int," +
                "projectScore int," +
                "projectCreateTime text," +
                "projectCompleteDate text," +
                "projectCompleteSpecificTime text," +
                "projectTimes int," +
                "projectTotalTimes int," +
                "projectOrder int," +
                "otherInfo text" +
                ")");
    }

    private void createAppTable(SQLiteDatabase db) {
        db.execSQL("create table " + APP_INFO_TABLE + "(" +
                "loginTimes int" +
                ")");
    }

    private void createGiftTable(SQLiteDatabase db) {
        db.execSQL("create table " + DBHelper.GIFT_TABLE_NAME + "(" +
                "id int identity(0,1)," +
                "giftName text," +
                "giftCreateTime text primary key," +
                "giftCompleteDate text," +
                "giftCompleteSpecificTime text," +
                "giftScore int," +
                "giftType int," +
                "isCompleted int," +
                "otherInfo text," +
                "giftOrder int" +
                ")");
    }

    private void createProjectTable(SQLiteDatabase db) {
        db.execSQL("create table " + DBHelper.PROJECT_TABLE_NAME + "(" +
                "id int identity(0,1)," +
                "projectName text," +
                "projectNote text," +
                "projectFixNote int," +
                "projectType int," +
                "projectScore int," +
                "projectCreateTime text primary key," +
                "projectCompleteDate text," +
                "projectCompleteSpecificTime text," +
                "projectTimes int," +
                "projectTotalTimes int," +
                "projectOrder int," +
                "otherInfo text" +
                ")");
    }

    private void createUserTable(SQLiteDatabase db) {
        db.execSQL("create table " + USER_TABLE_NAME + "(" +
                "id int identity(0,1)," +
                "phoneNumber text," +
                "nick text," +
                "password text," +
                "score int" +
                ")");
        db.execSQL("insert into " + USER_TABLE_NAME + "(phoneNumber,nick,password,score)" +
                " values (?,?,?,?)",new Object[]{"18620189581","Achilles","123456",0});
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


}
